package vip.web3.api.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.web3.api.annotation.Login;
import vip.web3.api.form.user.RechargeForm;
import vip.web3.api.form.user.RecordForm;
import vip.web3.api.form.user.TransferForm;
import vip.web3.api.form.user.WithdrawForm;
import vip.web3.api.service.base.SysConfigApiService;
import vip.web3.api.service.biz.OrderService;
import vip.web3.api.service.biz.RecordService;
import vip.web3.api.service.biz.UserWalletService;
import vip.web3.common.utils.R;
import vip.web3.common.utils.Servlets;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/asset")
public class AssetApi {

    @Resource
    private UserWalletService userWalletService;

    @Resource
    private OrderService orderService;

    @Resource
    private RecordService recordService;

    @Resource
    private SysConfigApiService sysConfigApiService;

    @Login
    @PostMapping("/records")
    public R buyRecords(@RequestBody RecordForm form) {
        return R.ok().put("data", this.recordService.records(form, Servlets.getUserId()));
    }

    @Login
    @PostMapping("/transfer")
    public R transfer(@Valid @RequestBody TransferForm form) {
        this.userWalletService.transfer(form);
        return R.ok();
    }

    @Login
    @PostMapping("/withdraw")
    public R withdraw(@Valid @RequestBody WithdrawForm form) {
        this.userWalletService.withdraw(form);
        return R.ok();
    }

    @Login
    @PostMapping("/chain/recharge")
    public R chain(@Valid @RequestBody RechargeForm form) {
        String value = sysConfigApiService.getValue("alloy:app:bsc:usdt:recharge");
        if("0".equals(value)){
            return R.error(-1, "暂时无法充值!");
        }
        this.recordService.recharge(form);
        return R.ok();
    }

    @Login
    @PostMapping("/recharge")
    public R recharge(@Valid @RequestBody RechargeForm form) {
        this.userWalletService.recharge(form);
        return R.ok();
    }

    @PostMapping("/recharge/notify")
    public R rechargeNotify(@RequestBody String xmlData) {
        log.error(xmlData);
        return R.ok();
    }

}
